local thbattle__ailisi_xiaoxiaojunshi = fk.CreateSkill {

  name = "thbattle__ailisi_xiaoxiaojunshi",

  tags = {},

}



thbattle__ailisi_xiaoxiaojunshi:addEffect(fk.EventPhaseEnd, {
  prompt = "#thbattle__ailisi_xiaoxiaojunshi",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(thbattle__ailisi_xiaoxiaojunshi.name) then
      return player.phase == Player.Play and table.find(player:getCardIds("he"), function(cid)
        return Fk:getCardById(cid).type == Card.TypeEquip
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, thbattle__ailisi_xiaoxiaojunshi.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targetPlayer
    local discard = room:askForDiscard(player, 1, 1, true, thbattle__ailisi_xiaoxiaojunshi.name, false, '.|.|.|.|.|equip',
      "#thbattle__ailisi_xiaoxiaojunshi-choose_card", true)
    if #discard > 0 then
      room:recastCard(discard, player, thbattle__ailisi_xiaoxiaojunshi.name)
      local discard_card = Fk:getCardById(discard[1]).sub_type
      if discard_card == Card.SubtypeArmor then
        room:notifySkillInvoked(player, thbattle__ailisi_xiaoxiaojunshi.name, "support")
        player:broadcastSkillInvoke(thbattle__ailisi_xiaoxiaojunshi.name, 3)
        targetPlayer = room:askForChoosePlayers(player, table.map(room.alive_players, function(p)
          if p:isWounded() then
            return p.id
          else
            return nil
          end
        end), 1, 1, "#thbattle__ailisi_xiaoxiaojunshi-recover", thbattle__ailisi_xiaoxiaojunshi.name, true)
        if #targetPlayer > 0 then
          room:recover({
            who = room:getPlayerById(targetPlayer[1]),
            num = 1,
            skillName = thbattle__ailisi_xiaoxiaojunshi.name,
            recoverBy = player,
          })
        end
      elseif discard_card == Card.SubtypeTreasure then
        room:notifySkillInvoked(player, thbattle__ailisi_xiaoxiaojunshi.name, "drawcard")
        player:broadcastSkillInvoke(thbattle__ailisi_xiaoxiaojunshi.name, 4)
        player:drawCards(1, thbattle__ailisi_xiaoxiaojunshi.name)
        room:addPlayerMark(player, "thbattle__ailisi_xiaoxiaojunshi-skipDiscard")
      elseif discard_card == Card.SubtypeWeapon then
        room:notifySkillInvoked(player, thbattle__ailisi_xiaoxiaojunshi.name, "offensive")
        player:broadcastSkillInvoke(thbattle__ailisi_xiaoxiaojunshi.name, 1)
        local tos_target = room:askToChoosePlayers(player, {
          targets = table.filter(room:getOtherPlayers(player), function(p, index, array)
            return player:canUseTo(Fk:cloneCard("danmu__slash"), p, { bypass_distances = true, bypass_times = true })
          end),
          skill_name = thbattle__ailisi_xiaoxiaojunshi.name,
          prompt = "#thbattle__ailisi_xiaoxiaojunshi-choose_slash",
          min_num = 1,
          max_num = 1,
          cancelable = true,
        })
        if #tos_target > 0 then
          room:useCard({
            from = player,
            tos = tos_target,
            card = Fk:cloneCard("danmu__slash"),
            extraUse = true,
          })
        end
      elseif discard_card == Card.SubtypeOffensiveRide or discard_card == Card.SubtypeDefensiveRide then
        room:notifySkillInvoked(player, thbattle__ailisi_xiaoxiaojunshi.name, "control")
        player:broadcastSkillInvoke(thbattle__ailisi_xiaoxiaojunshi.name, 2)
        local targets = table.filter(room:getOtherPlayers(player), function(p, index, array)
          if #p:getCardIds("e") > 0 and table.find(p:getCardIds("e"), function(cid)
                return Fk:getCardById(cid).sub_type == Card.SubtypeWeapon
              end) then
            return true
          end
        end)
        if #targets > 0 then
          local collateral = Fk:cloneCard("collateral")
          local p_ids = room:askForChoosePlayers(player, table.map(
            targets,
            Util.IdMapper
          ), 1, 1, "#thbattle__ailisi_xiaoxiaojunshi-jiedao", thbattle__ailisi_xiaoxiaojunshi.name)
          if #p_ids > 0 then
            local p = room:getPlayerById(p_ids[1])
            if player:isAlive() and player:canUseTo(collateral, p) then
              local filter_target = table.filter(
                room:getOtherPlayers(p),
                function(to)
                  return p:inMyAttackRange(to)
                end
              )
              if #filter_target > 0 then
                local availableTargets = table.map(
                  filter_target,
                  Util.IdMapper
                )
                if #availableTargets > 0 then
                  local tos = room:askForChoosePlayers(player, availableTargets, 1, 1,
                    "#thbattle__ailisi_xiaoxiaojunshi-jiedao", thbattle__ailisi_xiaoxiaojunshi.name)
                  if #tos > 0 then
                    room:useCard {
                      from = player,
                      tos = { p, room:getPlayerById(tos[1]) },
                      card = collateral,
                      skillName = thbattle__ailisi_xiaoxiaojunshi.name
                    }
                  end
                end
              end
            end
          end
        end
      end
    end
  end
})
thbattle__ailisi_xiaoxiaojunshi:addEffect(fk.EventPhaseChanging, {
  prompt = "#thbattle__ailisi_xiaoxiaojunshi",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(thbattle__ailisi_xiaoxiaojunshi.name) then
      return data.phase == Player.Discard and player:getMark("thbattle__ailisi_xiaoxiaojunshi-skipDiscard") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "thbattle__ailisi_xiaoxiaojunshi-skipDiscard")
    data.skipped = true
  end
})

return thbattle__ailisi_xiaoxiaojunshi
